<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="修改版本"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="900px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="使用日期" prop="startTime">
                <el-date-picker
                    v-model="timeRange"
                    type="daterange"
                    start-placeholder="生效日期"
                    end-placeholder="失效日期"
                    :disabled-date="disabledDate"
                />
            </el-form-item>

            <el-form-item label="备注" prop="remark">
                <el-input v-model="formData.remark"/>
            </el-form-item>

            <el-form-item label="配置信息" prop="sectionVersionConfig">
                <el-input v-model="formData.sectionVersionConfig" type="textarea" rows="20"/>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定修改版本吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">立即修改</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {LineSectionVersionApi} from '@/api/intercity/LineSectionVersionApi'

export default {
    name: 'LineSectionVersionUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: '',
                startTime: '',
                endTime: '',
                sectionVersionConfig: '',
                remark: ''
            },
            timeRange: [],
            rules: {},
            dialogVisible: false,
            rules: {
                timeRange: [
                    {required: true, message: '请选择生效时间', trigger: 'change'}
                ],
            }
        }
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            this.$nextTick(() => {
                this.formData = data
                this.timeRange =[new Date(data.startTime),new Date(data.endTime)]
                this.dialogVisible = true
            })
        },

        disabledDate(time) {
            return time.getTime() < Date.now()
        },

        // 提交表单
        submitForm() {
            const that = this
            const dataForm = that.formData
            dataForm.startTime = that.timeRange[0]
            dataForm.endTime = that.timeRange[1]

            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    LineSectionVersionApi.update(dataForm).then((response) => {
                        that.dialogVisible = false
                        that.$emit('refresh')
                    })
                }
            })
        }
    }
}
</script>
